
package com.scalar.chiptrack.operations.reports.actions;

import com.scalar.chiptrack.ChipTrackAction;
import com.scalar.chiptrack.operations.process.dao.ProductDao;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.commons.Constants;
import com.scalar.chiptrack.users.User;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;

/*
 * Created by ChandraSekhar.
 * Date: Jun 23, 2005
 * Time: 2:13:42 PM
 */
public class DieBankReportAction extends ChipTrackAction
{
    private static final String ACTION_NAME = "/dieBankReport.do";

    public ActionForward execute( ActionMapping mapping,
                                  ActionForm form,
                                  HttpServletRequest request,
                                  HttpServletResponse response )
            throws Exception
    {
        HttpSession session = request.getSession();
        ArrayList baseProductList   = null;
        ArrayList partNumberList    = null;
        int baseProductIdInt = 0;

        User user = ( User ) session.getAttribute( SessionKeys.USER_KEY );

        if ( user == null )
        {
            session.setAttribute( SessionKeys.SOURCE_PAGE_KEY, ACTION_NAME + "?" + request.getQueryString() );
            return ( mapping.findForward( "login" ) );
        }

        String action = request.getParameter( "action" );
        String baseProductId = request.getParameter("baseProductId");
        String reportType = request.getParameter( "reportType" );

        if ( action == null )
        {
            action = "";
        }
        request.setAttribute( "action", action );

        if ( "rmaWafersReport".equalsIgnoreCase( reportType )  )
        {
            ArrayList categoryList           = new ArrayList();

            categoryList.add( new org.apache.struts.util.LabelValueBean( Constants.MRBRELEASE_MOVE_TO_ENGG, Constants.MRBRELEASE_MOVE_TO_ENGG ) );
            categoryList.add( new org.apache.struts.util.LabelValueBean( Constants.MRBRELEASE_MOVE_TO_MATRIX, Constants.MRBRELEASE_MOVE_TO_MATRIX ) );
            categoryList.add( new org.apache.struts.util.LabelValueBean( Constants.MRBRELEASE_MOVE_TO_MRB, Constants.MRBRELEASE_MOVE_TO_MRB ) );
            categoryList.add( new org.apache.struts.util.LabelValueBean( Constants.MRBRELEASE_MOVE_TO_RMA, Constants.MRBRELEASE_MOVE_TO_RMA ) );
            categoryList.add( new org.apache.struts.util.LabelValueBean( Constants.MRBRELEASE_MOVE_TO_RTV, Constants.MRBRELEASE_MOVE_TO_RTV ) );
            categoryList.add( new org.apache.struts.util.LabelValueBean( Constants.MRBRELEASE_MOVE_TO_SCRAP, Constants.MRBRELEASE_MOVE_TO_SCRAP ) );

            if ( baseProductId != null && action.equalsIgnoreCase("loadPartNumbers") )
            {
                try
                {
                    baseProductIdInt = Integer.parseInt( baseProductId );
                    partNumberList = ProductDao.loadAllPartNumberByBaseProductIdAndProcessType( baseProductIdInt, Constants.LOCATION_TYPE_SORT);
                }
                catch( NumberFormatException nfe ) { }

            }
            else
            {
                partNumberList = new ArrayList();
            }

            baseProductList = ProductDao.loadChipBaseProducts( null );

            request.setAttribute( "baseProductList", baseProductList );
            request.setAttribute( "partNumberList", partNumberList );
            request.setAttribute( "categoryList", categoryList );

            return mapping.findForward( "rmaWafersReport" );
        }
        if ( baseProductId != null && action.equalsIgnoreCase("loadPartNumbers") )
        {
            try
            {
                baseProductIdInt = Integer.parseInt( baseProductId );
            }
            catch( NumberFormatException nfe ) { }
            if( "sortBinClosedLotReport".equalsIgnoreCase( reportType )  )
            {
                partNumberList = ProductDao.loadAllPartNumberByBaseProductIdAndProcessType( baseProductIdInt, Constants.LOCATION_TYPE_SORT);
            }
            else if ("fabDataReport".equalsIgnoreCase( reportType ) )
            {
                partNumberList = ProductDao.loadAllPartNumberByBaseProductIdAndProcessType( baseProductIdInt, Constants.LOCATION_TYPE_FAB);
            }
            else
            {
                partNumberList = ProductDao.loadAllPartNumberByBaseProductIdAndProcessType( baseProductIdInt, Constants.LOCATION_TYPE_DIE_BANK );
            }
        }
        else
        {
           partNumberList = new ArrayList();
        }

        baseProductList = ProductDao.loadChipBaseProducts( null );
        request.setAttribute( "baseProductList", baseProductList );
        request.setAttribute( "partNumberList", partNumberList );

        if( "sortBinClosedLotReport".equalsIgnoreCase( reportType )  )
        {
            return mapping.findForward( "sortBinClosedLotReport" );
        }
        else if ("fabDataReport".equalsIgnoreCase( reportType ) )
        {
            return mapping.findForward( "fabDataReport" );
        }
        else
        {
            return mapping.findForward( "success" );
        }

    }
}

